package murphyhan;

import java.io.UnsupportedEncodingException;

public class Placemark {
	Photo photo;
	Path path;
	String caption;
	GLatLng gLatLng;

	Placemark(GLatLng gLatLng) {
		this.gLatLng = gLatLng;
	}

	public String getPath(int division) {
		// path.toString()
		return "";
	}

	Placemark(Photo photo, Path path) {
		this.photo = photo;
		this.path = path;
		this.gLatLng = path.getLstPoint();

	}

	public String getName() {
		return photo == null ? "" : photo.fileName;
	}

	public String getDesc() {
		return photo == null ? "" : stringToUnicode(photo.caption);
	}

	public String getEle() {
		return gLatLng == null ? "" : String.valueOf(gLatLng.ele);
	}

	public String getTime() {
		java.text.SimpleDateFormat dateformat = new java.text.SimpleDateFormat(
				"HH:mm:ss yyyy-MM-dd");

		String time = dateformat.format(gLatLng.time);

		return gLatLng == null ? "" : time;
	}

	public String getMe(int seq) {

		StringBuffer re = new StringBuffer();
		re.append("{");
		re.append("seq:");
		// re.append("\"");
		re.append(seq);
		// re.append("\"");
		re.append(",");
		re.append("name:");
		re.append("\"");
		re.append(getName());
		re.append("\"");
		re.append(",");
		re.append("date:");
		re.append("\"");
		re.append(getTime());
		re.append("\"");
		re.append(",");
		re.append("ele:");
		re.append("\"");
		re.append(getEle());
		re.append("\"");
		re.append(",");
		re.append("point:");
		re.append(gLatLng.toString());
		re.append(",");
		re.append("desc:");
		re.append("\"");
		re.append(getDesc());
		re.append("\"");
		re.append("}");

		return re.toString();

	}

	public static String stringToUnicode(String str) {
		int countChar = str.length();
		StringBuffer sb = new StringBuffer();
		try {
			for (int i = 0; i < countChar; i++) {
				// don't transform '\r' , '\n'
				if (str.substring(i, i + 1).equals("\r")
						|| str.substring(i, i + 1).equals("\n")) {
					sb.append("\n");
				} else {
					// encode by using utf-16
					byte[] uni_str = str.substring(i, i + 1).getBytes("utf-16");
					sb.append("\\u");
					// start to transform to unicode
					for (int j = 2; j < uni_str.length; j++) {
						int tmp = uni_str[j];
						if (tmp < 0) {
							tmp = 256 + tmp;
							if (tmp < 16)
								sb.append("0" + Integer.toHexString(tmp));
							else
								sb.append(Integer.toHexString(tmp));
						} else if (tmp < 16) {
							sb.append("0" + Integer.toHexString(tmp));
						} else {
							sb.append(Integer.toHexString(tmp));
						}
					}
				}
			}

		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		return sb.toString();
	}

	public String toString() {
		return photo.toString() + " points=" + path;
	}

}
